using UnityEngine;

public class AdvancedShake : MonoBehaviour
{
	private IRageSpline rageSpline;

	private Vector3[] originalPositions;

	private Vector3[] targetPositions;

	public Vector2 shakeSizeInNormalSpace;

	public float minFrameGap = 0.2f;

	public float maxFrameGap = 0.5f;

	public float easing = 0.1f;

	private float currentShakeGap;

	private float timeSinceLastShake;

	private void Awake()
	{
		rageSpline = GetComponent(typeof(RageSpline)) as IRageSpline;
		targetPositions = new Vector3[rageSpline.GetPointCount()];
		originalPositions = new Vector3[rageSpline.GetPointCount()];
		for (int i = 0; i < rageSpline.GetPointCount(); i++)
		{
			originalPositions[i] = rageSpline.GetPosition(i);
		}
	}

	private void Update()
	{
		timeSinceLastShake += Time.deltaTime;
		if (timeSinceLastShake > currentShakeGap)
		{
			for (int i = 0; i < rageSpline.GetPointCount(); i++)
			{
				Vector3 vector = new Vector3(Random.Range(-0.5f * shakeSizeInNormalSpace.x, 0.5f * shakeSizeInNormalSpace.x), Random.Range(-0.5f * shakeSizeInNormalSpace.y, 0.5f * shakeSizeInNormalSpace.y), 0f);
				Vector3 normal = rageSpline.GetNormal(i);
				Vector3 vector2 = Vector3.Cross(normal, Camera.main.transform.forward);
				targetPositions[i] = originalPositions[i] + vector.x * vector2 + vector.y * normal;
			}
			currentShakeGap = Random.Range(minFrameGap, maxFrameGap);
			timeSinceLastShake = 0f;
		}
		else
		{
			for (int j = 0; j < rageSpline.GetPointCount(); j++)
			{
				Vector3 position = rageSpline.GetPosition(j);
				rageSpline.SetPoint(j, Vector3.Lerp(position, targetPositions[j], Time.deltaTime * (1f / easing)));
			}
		}
		rageSpline.RefreshMesh(true, true, false);
	}
}
